package com.doctorliang.cheng.shopping.home.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.base.BaseFragment;
import com.doctorliang.cheng.shopping.home.adapter.HomeFragmentAdapter;
import com.doctorliang.cheng.shopping.home.bean.ResultBeanData;
import com.doctorliang.cheng.shopping.user.activity.MessageCenterActivity;
import com.doctorliang.cheng.shopping.utils.Constants;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static android.content.ContentValues.TAG;

/**
 * Created by CHENG on 2017/9/26.
 * 主页视图的Fragment
 */

public class HomeFragment extends BaseFragment {
    private static final int SUCCESS = 1;
    private RecyclerView rvHome;
    private ImageView ib_top;
    private TextView tv_search_home;
    private TextView tv_message_home;
    OkHttpClient client = new OkHttpClient();
    private ResultBeanData.ResultBean results;
    private HomeFragmentAdapter adapter ;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SUCCESS:
                    //绑定适配器
                    if (results != null) {
                        //有数据
                        adapter = new HomeFragmentAdapter(mcontext,results);
                        rvHome.setAdapter(adapter);
                        GridLayoutManager gridLayoutManager = new GridLayoutManager(mcontext, 1);
                        rvHome.setLayoutManager(gridLayoutManager);
                        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                            @Override
                            public int getSpanSize(int position) {
                                if (position <= 3) {
                                    //隐藏回到顶部的按钮
                                    ib_top.setVisibility(View.GONE);
                                } else {
                                    //显示回到顶部的按钮
                                    ib_top.setVisibility(View.VISIBLE);
                                }
                                //只能返回1
                                return 1;
                            }
                        });
                    } else {
                        //没数据
                    }
            }
        }
    };
    @Override
    public View initView() {
        View view = View.inflate(mcontext, R.layout.fragment_home,null);
        rvHome = (RecyclerView) view.findViewById(R.id.rv_home);
        ib_top = (ImageView) view.findViewById(R.id.ib_top);
        tv_search_home = (TextView) view.findViewById(R.id.tv_search_home);
        tv_message_home = (TextView) view.findViewById(R.id.tv_message_home);
        Log.e(TAG,"主页视图被初始化了");
        initListener();
        return view;
    }
    private void initListener() {
        //置顶的监听
        ib_top.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rvHome.scrollToPosition(0);
            }
        });

        //搜素的监听
        tv_search_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(mcontext, "搜索", Toast.LENGTH_SHORT).show();
            }
        });

        //消息的监听
        tv_message_home.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mcontext, MessageCenterActivity.class);
                mcontext.startActivity(intent);
            }
        });

    }
    @Override
    public void initData() {
        super.initData();
        Log.e(TAG,"主页视图的数据被初始化了");
        getDataFromNet();
    }

    private void getDataFromNet() {
        new Thread(){
            @Override
            public void run() {
                super.run();
                String response = null;
                response = run(Constants.HOME_URL);
                //解析json
                parseJson(response);
            }

            private String run(String homeUrl) {
                if (homeUrl!=null) {
                    Request request = new Request.Builder()
                            .url(homeUrl)
                            .build();

                    Response response = null;
                    try {
                        response = client.newCall(request).execute();
                        System.out.println(response);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    try {
                        return response.body().string();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }


        }.start();

    }
    private void parseJson(String json) {
        ResultBeanData resultBeanData = JSON.parseObject(json, ResultBeanData.class);
        results = resultBeanData.getResult();
        String name = results.getAct_info().get(0).getName();
        Log.e(TAG,"解析成功=="+ name);
        Message msg = Message.obtain();
        msg.what = SUCCESS;
        handler.sendMessage(msg);


    }


}
